package com.weidey.blogWeb.controller;

import com.weidey.blog.domain.Group;
import com.weidey.blog.domain.Label;
import com.weidey.blog.service.IGroupService;
import com.weidey.blog.service.ILabelService;
import com.weidey.blogWeb.domain.WebArticle;
import com.weidey.blogWeb.domain.WebGroup;
import com.weidey.blogWeb.domain.param.UserArticleParam;
import com.weidey.blogWeb.service.ILikeService;
import com.weidey.blogWeb.service.IWebArticleService;
import com.weidey.blogWeb.service.IWebGroupService;
import com.weidey.common.core.controller.BaseController;
import com.weidey.common.core.domain.AjaxResult;
import com.weidey.common.utils.SecurityUtils;
import jakarta.annotation.Resource;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.core.task.AsyncTaskExecutor;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import java.util.List;

/**
 * 文章Controller
 */
//@Api(tags = "文章")
@RestController
@RequestMapping("/blog/web/article")
public class WebArticleController extends BaseController {
    @Resource
    IWebArticleService blogWebArticleService;

    @Resource
    ILabelService blogLabelService;

    @Resource
    IGroupService blogGroupService;

    @Resource
    IWebGroupService blogWebGroupService;

    @Resource
    ILikeService blogLikeService;



//    @ApiOperation("获取文章列表")
    @GetMapping("/select/list")
    public AjaxResult list(@Validated UserArticleParam userArticleParam){
        // 获取请求文章类型
        String type =  userArticleParam.getType();
        // 获取分组列表
        List<WebGroup> blogGroup = blogWebGroupService.getBlogGroup();
        //开始分页
        startPage();
        //根据文章类型获取文章列表
        for (WebGroup blogWebGroup : blogGroup) {
            String groupName = blogWebGroup.getGroup();
            if (groupName.equals(type)) {
                int groupId = blogWebGroup.getId().intValue();
                List<WebArticle> blogWebArticleList = blogWebArticleService.selectBlogArticleByGroup(groupId);
                //根据文章id查询博客文章标签列表
                blogWebArticleList =   blogWebArticleService.additional(blogWebArticleList);
                return AjaxResult.success(getDataTable(blogWebArticleList));
            }
        }
        return AjaxResult.error("没有该分组");
    }


//    @ApiOperation("获取文章热门列表")
    @GetMapping("/select/hot")
    public AjaxResult listHot(){
        return AjaxResult.success(getDataTable(blogWebArticleService.selectBlogArticleHot()));
    }

//    @ApiOperation("获取文章推荐列表")
    @GetMapping("/get/recommend")
    public AjaxResult listRecommend(@RequestParam(value ="userId") int userId){
        return AjaxResult.success(getDataTable(blogWebArticleService.userCfRecommend(userId)));
    }


//    @ApiOperation("获取文章详情")
    @GetMapping("/select/detail")
    public AjaxResult detail(@RequestParam(value = "articleId") int articleId){
         WebArticle blogWebArticle = blogWebArticleService.selectBlogArticleById(articleId);
         //根据文章id查询博客文章标签列表
         List<Label> labelList = blogLabelService.selectBlogLabelListById((long) articleId);
         //根据文章id查询博客文章分组
         Group group =  blogGroupService.selectBlogGroupById(blogWebArticle.getGroupId());
         //查询平均评分
         blogWebArticle.setRate(blogWebArticleService.selectRate(articleId));
         //查询点赞量
         blogWebArticle.setLikes(blogLikeService.getCount(1, (long) articleId));
         //查询收藏量
         blogWebArticle.setStar(blogLikeService.getCount(2, (long) articleId));
         //查询浏览量
          blogWebArticleService.updateViewCount(articleId);
         //将标签列表和分组添加到文章对象中
         blogWebArticle.setGroup(group);
         blogWebArticle.setLabels(labelList);
        return AjaxResult.success(blogWebArticle);
    }

//    @ApiOperation("文章评分")
    @GetMapping("/rate")
    public AjaxResult articleRate(@RequestParam("rate")  int rate, @RequestParam("articleId") int articleId){
        Long userId = SecurityUtils.getUserId();
        return blogWebArticleService.beginArticleRate(userId,articleId,rate);
    }

}
